package com.yurun.loginmodule.config;

import com.yurun.commonapi.api.CommonResult;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局拦截
 * 对entity的进行全局拦截，数据为空返回message
 */
@ControllerAdvice
public class ValidExceptionConfig {

    @ResponseBody
    @ExceptionHandler(BindException.class)
    public CommonResult throwCustomException(BindException  methodArgumentNotValidException){
        return CommonResult.failed(methodArgumentNotValidException.getBindingResult().getFieldError().getDefaultMessage());
    }
}